package com.ross.web.dao;

import java.util.LinkedList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.AliasToBeanResultTransformer;

import com.ross.web.domain.Orders;
import com.ross.web.domain.Product;
import com.ross.web.util.HibernateUtil;

public class OrdersDAO extends HibernateUtil {
	
	public List getAllOrders(String isconfirmed){
		
		Session session = HibernateUtil.getSession();
		List result = new LinkedList();
		
		try{
			
			result = session.createCriteria(Orders.class, "order")
				.setProjection(Projections.projectionList()
						.add(Projections.property("order.oid"),"oid")
						.add(Projections.property("order.prodId"),"prodId")
						.add(Projections.property("order.allproducts"),"allproducts")
						.add(Projections.property("order.orderdate"),"orderdate")
						.add(Projections.property("order.ordergrandtotal"),"ordergrandtotal")
						.add(Projections.property("order.isconfirmed"),"isconfirmed")
						.add(Projections.property("order.ordertime"),"ordertime")
						.add(Projections.property("order.confirmdate"),"confirmdate")
						.add(Projections.property("order.confirmtime"),"confirmtime")
						.add(Projections.property("order.customer_id"),"customer_id")
						.add(Projections.property("order.customer"),"customer")
						)
						
						.add(Restrictions.eq("order.isconfirmed", isconfirmed))
						
						.setResultTransformer(new AliasToBeanResultTransformer(Orders.class))
						.list();
			
			
			
		return result;
			
		}finally {
			session.close();
		}		
	}
	
	public Orders getOrdersById(int oid){
		
		Session session = HibernateUtil.getSession();
		List result = new LinkedList();
		
		try{	
			result = session.createCriteria(Orders.class, "order")
				.setProjection(Projections.projectionList()
						.add(Projections.property("order.oid"),"oid")
						.add(Projections.property("order.prodId"),"prodId")
						.add(Projections.property("order.allproducts"),"allproducts")
						.add(Projections.property("order.orderdate"),"orderdate")
						.add(Projections.property("order.ordergrandtotal"),"ordergrandtotal")
						.add(Projections.property("order.isconfirmed"),"isconfirmed")
						.add(Projections.property("order.ordertime"),"ordertime")
						.add(Projections.property("order.confirmdate"),"confirmdate")
						.add(Projections.property("order.confirmtime"),"confirmtime")
						.add(Projections.property("order.customer_id"),"customer_id")
						.add(Projections.property("order.customer"),"customer")
						)
						
						.add(Restrictions.eq("order.oid", oid))
						
						.setResultTransformer(new AliasToBeanResultTransformer(Orders.class))
						.list();
			
			
			
		return result.isEmpty() ? null : (Orders) result.get(0);
			
		}finally {
			session.close();
		}		
	}
	
	public void insert(Orders orders){
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		
		try{
			tx = session.beginTransaction();
			session.save(orders);
			tx.commit();
		}catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}
		
	}
	
	public void update(Orders orders){
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		
		try{
			tx = session.beginTransaction();
			session.update(orders);
			tx.commit();
		}catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}
		
	}
	
	public void delete(Orders orders){
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		
		try{
			tx = session.beginTransaction();
			session.delete(orders);
			tx.commit();
		}catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}
		
	}
}
